class Solution
{
private:
    void dfs(int i, vector<vector<int>>& isConnected)
    {
        for (int j = 0; j < isConnected.size(); ++j)
        {
            if (isConnected[i][j] == 1)
            {
                isConnected[i][j] = 2;
                dfs(j, isConnected);
            }
        }
    }

public:
    int findCircleNum(vector<vector<int>>& isConnected)
    {
        if (isConnected.empty() || isConnected[0].empty())
            return 0;

        int ans = 0;
        for (int i = 0; i < isConnected.size(); ++i)
            for (int j = 0; j < isConnected.size(); ++j)
                if (isConnected[i][j] == 1)
                {
                    dfs(i, isConnected);
                    ++ans;
                }

        return ans;
    }
};